<template>
  <el-dialog
    title="选择风控计划"
    :model-value="modelValue"
    @close="closed"
    width="1600px"
  >
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="160px"
    >
      <el-form-item label="计划编号" prop="planNo">
        <el-input
          v-model="queryParams.planNo"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="电压等级" prop="voltageLevel">
        <el-select
          v-model="queryParams.voltageLevel"
          placeholder="电压等级"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dl_voltage_level"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="作业组织（建管）单位" prop="jobGroupName">
        <el-tree-select
          v-model="queryParams.jobGroupName"
          :data="deptOptions"
          :props="{ value: 'label', label: 'label', children: 'children' }"
          value-key="label"
          placeholder="请选择"
          check-strictly
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery">查询</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="planList">
      <el-table-column
        label="作业组织（建管）单位"
        align="center"
        prop="jobGroupName"
      />
      <el-table-column label="电压等级" align="center" prop="voltageLevel" />
      <el-table-column
        label="作业风险等级"
        align="center"
        prop="jobRiskLevel"
      />
      <el-table-column
        label="电网风险等级"
        align="center"
        prop="powerGridRiskLevel"
      />
      <el-table-column label="计划状态" align="center" prop="planStatus" />
      <el-table-column label="工作负责人" align="center" prop="workHeaderName" />
      <el-table-column
        label="计划开始时间"
        align="center"
        prop="planStartDate"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.planStartDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="计划结束时间"
        align="center"
        prop="planEndDate"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.planEndDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划编号" align="center" prop="planNo" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding"
        width="100"
      >
        <template #default="scope">
          <el-button type="text" @click="handleSelect(scope.row)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closed">关闭</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { listPlan } from "@/api/platform/riskControlPlan";

import { deptTreeSelect } from "@/api/system/user";

const { proxy } = getCurrentInstance();

const {
  dl_voltage_level,
  dl_job_risk_level,
  dl_power_grid_risk_level,
  dl_job_type,
  dl_plan_status,
} = proxy.useDict(
  "dl_voltage_level",
  "dl_job_risk_level",
  "dl_power_grid_risk_level",
  "dl_job_type",
  "dl_plan_status"
);

defineProps({
  modelValue: {
    type: Boolean,
    required: true,
  },
});


const emits = defineEmits(["update:modelValue", "select"]);


const planList = ref([]);
const deptOptions = ref(undefined);


const loading = ref(false);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    jobGroupName: null,
    voltageLevel: null,
    planNo: null,
  },
});

const { queryParams } = toRefs(data);

/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then((response) => {
    deptOptions.value = response.data;
  });
}

/** 查询风控计划列表 */
function getList() {
  loading.value = true;
  listPlan(queryParams.value).then((response) => {
    planList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function handleSelect(row) {
    emits('select', row)
    emits("update:modelValue", false);
}

/*** 关闭*/
const closed = () => {
  emits("update:modelValue", false);
};

getDeptTree();

getList()

</script>